<?php
namespace Home\Controller;
use Think\Controller;
use Org\Util;

class PushController extends Controller {
    private $WeixinLib = null;
    public function _initialize(){
//        parent::_initialize();
        $this->WeixinLib = new Util\WeiXin();
    }
    public function wxPush(){
        $Ae = new Util\AEQuery('ec','gr_push_user');
        $count = $Ae->count("flag=0 and report_day='2016-06-14'");
        if($count>0){
            $where = "flag=0 and report_day='2016-06-14'";
            $user = $Ae->find($where,50);
            $result = Util\AETools::toArray($user);
            for($i=0;$i<count($result);$i++){
                $r =$this->sendWxMesseage($result[$i]['openid']);
                if($r->errcode === 0){
                    $Ae = new Util\AEQuery('ec','gr_push_user');
                    $AEObject = $Ae->get($result[$i]['id']);
                    $AEObject->set('flag',1);
                    $AEObject->save();
                }
                if($r->errcode === 43004){
                    $Ae = new Util\AEQuery('ec','gr_push_user');
                    $AEObject = $Ae->get($result[$i]['id']);
                    $AEObject->set('flag',-1);
                    $AEObject->save();
                }
                if($r->errcode === 40001){
                    $this->WeixinLib->getAccessToken(true);
                }
            }
            echo 'success';

        }else{
            echo 'over';
        }
    }
    public function wxPush1(){
        $Ae = new Util\AEQuery('ec','gr_push_user');
        $count = $Ae->count("flag=0 and report_day='2016-06-16'");
        if($count>0){
            $where = "flag=0 and report_day='2016-06-16'";
            $user = $Ae->find($where,50);
            $result = Util\AETools::toArray($user);
            for($i=0;$i<count($result);$i++){
                $r =$this->sendWxMesseage($result[$i]['openid']);
                if($r->errcode === 0){
                    $Ae = new Util\AEQuery('ec','gr_push_user');
                    $AEObject = $Ae->get($result[$i]['id']);
                    $AEObject->set('flag',1);
                    $AEObject->save();
                }
                if($r->errcode === 43004){
                    $Ae = new Util\AEQuery('ec','gr_push_user');
                    $AEObject = $Ae->get($result[$i]['id']);
                    $AEObject->set('flag',-1);
                    $AEObject->save();
                }
                if($r->errcode === 40001){
                    $this->WeixinLib->getAccessToken(true);
                }
            }
            echo 'success';

        }else{
            echo 'over';
        }
    }

    public function wxPush2(){
        $day= I('day');
        $bin= I('bin');

        if($day==''){
            echo 'err:day is null';
            return;
        }

        if($bin==''){
            echo 'err:bin is null';
            return;
        }

        $Ae_pushed = new Util\AEQuery('ec','gr_pushed');
        $count_pushed = $Ae_pushed->count("status=1 and bin = '".$bin."'");

        if($count_pushed == 0){
            echo 'err:bin value is null';
            return;
        }

        $Ae = new Util\AEQuery('ec','gr_push_user');
        $count = $Ae->count("flag=0 and bin = '".$bin."' and report_day='".$day."'");
        if($count>0){
            $where = "flag=0 and bin = '".$bin."' and report_day='".$day."'";
            $user = $Ae->find($where,50);
            $result = Util\AETools::toArray($user);
            for($i=0;$i<count($result);$i++){
                $r =$this->sendWxMesseage($result[$i]['openid'],$bin);
                if($r->errcode === 0){
                    $Ae = new Util\AEQuery('ec','gr_push_user');
                    $AEObject = $Ae->get($result[$i]['id']);
                    $AEObject->set('flag',1);
                    $AEObject->save();
                }
                if($r->errcode === 43004){
                    $Ae = new Util\AEQuery('ec','gr_push_user');
                    $AEObject = $Ae->get($result[$i]['id']);
                    $AEObject->set('flag',-1);
                    $AEObject->save();
                }
                if($r->errcode === 40001){
                    $this->WeixinLib->getAccessToken(true);
                }
            }
            echo 'success';

        }else{
            echo 'over';
        }
    }

    public function wxPush3(){
        $day= I('day');
        $bin= I('bin');

        if($day==''){
            echo 'err:day is null';
            return;
        }

        if($bin==''){
            echo 'err:bin is null';
            return;
        }

        $Ae_pushed = new Util\AEQuery('ec','gr_pushed');
        $count_pushed = $Ae_pushed->count("status=1 and bin = '".$bin."'");

        if($count_pushed == 0){
            echo 'err:bin value is null';
            return;
        }

        $Ae = new Util\AEQuery('ec','gr_push_user');
        $count = $Ae->count("flag=0 and bin = '".$bin."' and report_day='".$day."'");
        if($count>0){
            $where = "flag=0 and bin = '".$bin."' and report_day='".$day."'";
            $user = $Ae->find($where,50);
            $result = Util\AETools::toArray($user);
            for($i=0;$i<count($result);$i++){
                $r =$this->sendWxMesseage3($result[$i]['openid'],$bin);
                if($r->errcode === 0){
                    $Ae = new Util\AEQuery('ec','gr_push_user');
                    $AEObject = $Ae->get($result[$i]['id']);
                    $AEObject->set('flag',1);
                    $AEObject->save();
                }
                if($r->errcode === 43004){
                    $Ae = new Util\AEQuery('ec','gr_push_user');
                    $AEObject = $Ae->get($result[$i]['id']);
                    $AEObject->set('flag',-1);
                    $AEObject->save();
                }
                if($r->errcode === 40001){
                    $this->WeixinLib->getAccessToken(true);
                }
            }
            echo 'success';

        }else{
            echo 'over';
        }
    }
    public function wxPush4(){
        $day= I('day');
        $bin= I('bin');

        if($day==''){
            echo 'err:day is null';
            return;
        }

        if($bin==''){
            echo 'err:bin is null';
            return;
        }

        $Ae_pushed = new Util\AEQuery('ec','gr_pushed');
        $count_pushed = $Ae_pushed->count("status=1 and bin = '".$bin."'");

        if($count_pushed == 0){
            echo 'err:bin value is null';
            return;
        }

        $Ae = new Util\AEQuery('ec','gr_push_user');
        $count = $Ae->count("flag=0 and bin = '".$bin."' and report_day='".$day."'");
        if($count>0){
            $where = "flag=0 and bin = '".$bin."' and report_day='".$day."'";
            $user = $Ae->find($where,50);
            $result = Util\AETools::toArray($user);
            for($i=0;$i<count($result);$i++){
                $r =$this->sendWxMesseage4($result[$i]['openid'],$bin);
                if($r->errcode === 0){
                    $Ae = new Util\AEQuery('ec','gr_push_user');
                    $AEObject = $Ae->get($result[$i]['id']);
                    $AEObject->set('flag',1);
                    $AEObject->save();
                }
                if($r->errcode === 43004){
                    $Ae = new Util\AEQuery('ec','gr_push_user');
                    $AEObject = $Ae->get($result[$i]['id']);
                    $AEObject->set('flag',-1);
                    $AEObject->save();
                }
                if($r->errcode === 40001){
                    $this->WeixinLib->getAccessToken(true);
                }
            }
            echo 'success';

        }else{
            echo 'over';
        }
    }
    public function wxPushTest(){

        $bin= I('bin');

        if($bin==''){
            echo 'err:bin is null';
            return;
        }

        $Ae_pushed = new Util\AEQuery('ec','gr_pushed');
        $count_pushed = $Ae_pushed->count("status=1 and bin = '".$bin."'");

        if($count_pushed == 0){
            echo 'err:bin value is null';
            return;
        }

        $Ae = new Util\AEQuery('ec','gr_push_user');
        $count = $Ae->count("flag=0 and bin = 'ceshi' and report_day = '2016-05-28'");
        if($count>0){
            $where = "flag=0 and bin = 'ceshi' and report_day = '2016-05-28'";
            $user = $Ae->find($where,50);
            $result = Util\AETools::toArray($user);
            for($i=0;$i<count($result);$i++){
                $r =$this->sendWxMesseage($result[$i]['openid'],$bin);
                if($r->errcode === 0){
                    $Ae = new Util\AEQuery('ec','gr_push_user');
                    $AEObject = $Ae->get($result[$i]['id']);
                    $AEObject->set('flag',1);
                    $AEObject->save();
                }
                if($r->errcode === 43004){
                    $Ae = new Util\AEQuery('ec','gr_push_user');
                    $AEObject = $Ae->get($result[$i]['id']);
                    $AEObject->set('flag',-1);
                    $AEObject->save();
                }
                if($r->errcode === 40001){
                    $this->WeixinLib->getAccessToken(true);
                }
            }
            echo 'success';

        }else{
            echo 'over';
        }
    }
    public function sendWxMesseage($openid='',$bin){
        $WX_TMPLMSG=C('WX_TMPLMSG');

        $Ae_pushed = new Util\AEQuery('ec','gr_pushed');
        $where = "status=1 and bin = '".$bin."'";
        $pushed = $Ae_pushed->find($where,1);
        $result = Util\AETools::toArray($pushed);

        $param1 = $result[0]['param1'];
        $param2 = $result[0]['param2'];
        $param3 = $result[0]['param3'];
        $param4 = $result[0]['param4'];

//        $Data['first'] = array("value" => "来自无锡阳山的软萌妹纸，嫩的一掐出水，甜到入口即化，首批尝鲜1000份已售罄，第二批正挂在枝头接受预订。车厘子、水蜜桃、蓝莓，夏天就应该是这样甜甜的味道，你觉得呢？", "color" => "#173177");
//        $Data['keyword2'] = array("value" => '无锡阳山水蜜桃，美国车厘子，精选蓝莓。', "color" => "#FF5D00");
//        $Data['keyword1'] = array("value" =>  "筋斗云特快专递", "color" => "#173177");
//        $Data['remark'] = array("value" => '>>> 点这里，还有免费试吃哦', "color" => "#173177");

        $Data['first'] = array("value" => $param1, "color" => "#173177");
        $Data['keyword1'] = array("value" =>  $param2, "color" => "#173177");
        $Data['keyword2'] = array("value" => $param3, "color" => "#FF5D00");
        $Data['remark'] = array("value" => $param4, "color" => "#173177");

        $r=$this->WeixinLib->sendWxMessages($WX_TMPLMSG['到货通知'], $Data, $openid, "http://" . $_SERVER['HTTP_HOST'] . U('Activity/pushpage?bin='.$bin)); //发送微信消息
        return $r;
    }

    public function sendWxMesseage3($openid='',$bin){
        $WX_TMPLMSG=C('WX_TMPLMSG');

        $Data['first'] = array("value" => "五颗杨梅，擦去闷热的感觉，送你一道杨梅汤秘方", "color" => "#173177");
        $Data['keyword1'] = array("value" =>  "爱杨梅的你", "color" => "#173177");
        $Data['keyword2'] = array("value" => '闲下来，做碗汤', "color" => "#173177");
        $Data['keyword3'] = array("value" =>  "秘方不要钱", "color" => "#173177");
        $Data['remark'] = array("value" => '>>> 填写问卷，还送优惠卷哦！', "color" => "#FF5D00");

        $r=$this->WeixinLib->sendWxMessages($WX_TMPLMSG['阅读提醒'], $Data, $openid, "http://" . $_SERVER['HTTP_HOST'] . U('Activity/pushpage?bin='.$bin)); //发送微信消息
        return $r;
    }
    public function sendWxMesseage4($openid='',$bin){
        $WX_TMPLMSG=C('WX_TMPLMSG');

        $Data['first'] = array("value" => "美国JJ车厘子！！！是的，今天的特价水果是大家最期待的车厘子。只有150份，先到先得哦!", "color" => "#D80000");
        $Data['keyword1'] = array("value" =>  "爱吃水果的你", "color" => "#173177");
        $Data['keyword2'] = array("value" => '想吃水果的时候', "color" => "#173177");
        $Data['keyword3'] = array("value" =>  "美国JJ车厘子1斤38元", "color" => "#173177");
        $Data['remark'] = array("value" => '>>>更多特价水果，点击查看', "color" => "#FF5D00");

        $r=$this->WeixinLib->sendWxMessages($WX_TMPLMSG['阅读提醒'], $Data, $openid, "http://" . $_SERVER['HTTP_HOST'] . U('Activity/pushpage?bin='.$bin)); //发送微信消息
        return $r;
    }

}